/**
 * UIView+GTExtensions.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import "UIView+GTExtension.h"

@implementation UIView (GTExtension)

+ (id)viewFillingApplicationFrame {
  return [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];  
}

+ (id)viewUnderNavigationBar:(UIViewController*)aController {
  CGRect appBounds = [[UIScreen mainScreen] applicationFrame];
  CGRect areaUnderNavigationBar = CGRectMake(0.0, 0.0, appBounds.size.width, appBounds.size.height - aController.navigationController.navigationBar.frame.size.height);
  return [[[UIView alloc] initWithFrame:areaUnderNavigationBar] autorelease];  
}

+ (id)viewUnderNavigationBar:(UIViewController*)aController withBackgroundImageNamed:(NSString*)name {
  UIView *view = [UIView viewUnderNavigationBar:aController];
  view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:name]];
  return view;
}

+ (id)viewWrappingView:(UIView*)aView withBorderWidth:(CGFloat)borderWidth {
  UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, aView.frame.size.width + (borderWidth*2), aView.frame.size.height + (borderWidth*2))] autorelease];
  aView.frame = CGRectMake(borderWidth, borderWidth, aView.frame.size.width, aView.frame.size.height);
  [view addSubview:aView];
  return view;
}

- (id)initForFill:(UIView *)aView {
  CGRect viewFrame = CGRectMake(0.0, 0.0, aView.frame.size.width, aView.frame.size.height);
  return [self initWithFrame:viewFrame];
}

- (void)putAtTheTop {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            0.0,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putAtTheBottom {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            self.superview.frame.size.height - self.frame.size.height,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putOffAtTheTop {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            -self.frame.size.height,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putOffAtTheBottom {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            self.superview.frame.size.height,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putAtSameOriginOfView:(UIView*)anotherView {
  [self setFrame:CGRectMake(anotherView.frame.origin.x,
                            anotherView.frame.origin.y,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (BOOL)isOff {
  BOOL isOffAtTheTop = self.frame.origin.y == -self.frame.size.height;
  BOOL isOffAtTheBottom = self.frame.origin.y == self.superview.frame.size.height;
  return isOffAtTheTop || isOffAtTheBottom;
}

- (void)putAtTheTopAndFillSuperview {
  [self setFrame:CGRectMake(0.0,
                            0.0,
                            self.superview.frame.size.width,
                            self.superview.frame.size.height)];
}

- (void)putToFillApplicationFrame {
  [self setFrame:[[UIScreen mainScreen] applicationFrame]];
}

- (void)putUnder:(UIView*)anotherView {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            anotherView.frame.origin.y + anotherView.frame.size.height,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putOver:(UIView*)anotherView {
  [self setFrame:CGRectMake(self.frame.origin.x,
                            anotherView.frame.origin.y - self.frame.size.height,
                            self.frame.size.width,
                            self.frame.size.height)];
}

- (void)putAndFillBetweenUpperView:(UIView*)upperView lowerView:(UIView*)lowerView {
  [self setFrame:CGRectMake(0.0,
                            upperView.frame.origin.y + upperView.frame.size.height,
                            320.0,
                            lowerView.frame.origin.y - (upperView.frame.origin.y + upperView.frame.size.height))];
}

- (void)putAndFillUnderUpperView:(UIView*)upperView {
  [self setFrame:CGRectMake(0.0,
                            upperView.frame.origin.y + upperView.frame.size.height,
                            320.0,
                            self.superview.frame.size.height - (upperView.frame.origin.y + upperView.frame.size.height))];
}

- (void)putAndFillOverLowerView:(UIView*)lowerView {
  [self setFrame:CGRectMake(0.0,
                            0.0,
                            320.0,
                            lowerView.frame.origin.y)];
}

@end
